// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using Microsoft.WindowsMobile.Status;
using svs.Mobile.BeyondScheduler.Core;
using svs.Mobile.BeyondScheduler.Core.Objects;
using svs.Mobile.BeyondScheduler.Core.Objects.Operations;

namespace svs.Mobile.BeyondScheduler.Core.Objects
{
	public class ScheduledTask
	{
		protected bool _Active = true;
		private Event _Event;
		private BatteryLevel _MinBatteryLevelRequired = BatteryLevel.VeryLow;
		private OperationBase _Operation;
		private string _TaskID;

		public ScheduledTask(Event @event, OperationBase operation)
		{
			_Event = @event;
			Operation = operation;
		}


		public ScheduledTask(string actionID)
		{
			_TaskID = actionID;
		}

		public string TaskID
		{
			get
			{
				if (_TaskID == null)
					_TaskID = TaskRepository.GenerateUniqueTaskID();

				return _TaskID;
			}
		}

		public int TaskNumber
		{
			get
			{
				return TaskRepository.GetTaskNumberFromID(TaskID);
			}
		}

		/// <summary>
		/// True if the action is active
		/// </summary>
		public bool Active
		{
			get { return _Active; }
			set { _Active = value; }
		}

		public Event Event
		{
			get { return _Event; }
			set { _Event = value; }
		}

		public OperationBase Operation
		{
			get { return _Operation; }
			set
			{
				 _Operation = value;
				value.Owner = this;
			}
		}

		public BatteryLevel MinBatteryLevelRequired
		{
			get { return _MinBatteryLevelRequired; }
			set { _MinBatteryLevelRequired = value; }
		}

		/// <summary>
		/// True if the task can be executed at this time
		/// </summary>
		public bool CheckIfCanExecute()
		{
			if (!Active)
			{
				CoreHelper.WriteLogEntry(string.Format(" Task {0} is Inactive!", this));
				return false;
			}

			int batRequired = (int) MinBatteryLevelRequired;
			int batCurrent = (int) SystemState.PowerBatteryStrength;

			if(SystemState.PowerBatteryState == BatteryState.Charging)
				return true;

			if (batCurrent < batRequired)
			{
				CoreHelper.WriteLogEntry(string.Format(" Task {0} requires battery level of at least {1} %! Current battery level: {2} %!", this, batRequired, batCurrent));
				return false;
			}

			return true;
		}

		public override string ToString()
		{
			return
				string.Format("{0}, {1}, {2} ({3}-{4})", Operation, Event, Active ? "" : "Inactive!", Event.EventHandle, TaskID);
		}

		public static int Compare(ScheduledTask lhs, ScheduledTask rhs)
		{
			if ((lhs.Event == null || lhs.Event.StartTime.HasValue == false) &&
			    (rhs.Event == null || rhs.Event.StartTime.HasValue == false))
				return 0;

			if (lhs.Event == null || lhs.Event.StartTime.HasValue == false)
				return -1;

			if (rhs.Event == null || rhs.Event.StartTime.HasValue == false)
				return 1;

			return DateTime.Compare((DateTime) lhs.Event.StartTime, (DateTime) rhs.Event.StartTime);
		}
	}
}